/**
 *  uart.c - A serial driver sample for device driver framwork.
 *
 *  Copyright (C) 2008-2009 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "include/types.h"
#include "include/mem.h"
#include "include/sem.h"
#include "include/irq.h"
#include "drivers/chdev.h"
#include "hw_uart.h"

uart_t *puart0;
static word_t uart0_init(struct uart_mode Mode) {
    buf_t *pbufr;
    sem_t *pmutex;

    if((puart0 = (uart_t*)kmalloc(sizeof(uart_t))) == NULL) {
        return FALSE;
    }

    if((pbufr = bufInit(32)) == NULL) {
        kfree(puart0);
        return FALSE;
    }

    if((pmutex = semInitMutex(UART0_SEM_NO)) == NULL) {
        kfree(puart0);
        bufDel(pbufr);
        return FALSE;
    }

    puart0->psem = pmutex;
    puart0->prevbuf = pbufr;
    puart0->psndbuf = NULL;

    init_uart_args(Mode);
    return TRUE;
}

static word_t uart0_open(void) {
    semGain(0, puart0->psem);

    return TRUE;
}

static word_t uart0_close(void) {
    semPost(0, puart0->psem);

    return TRUE;
}

static word_t uart0_write(const char_t *buf, uword_t len, loff_t loff) {
    word_t i;

    if(buf == NULL) {
        return 0;
    }

    for(i=0;i<len;i++) {
        if(uart_send_byte(buf[i]) != TRUE) {
            break;
        }
    }
    return i;
}

static word_t uart0_read(char_t *buf, uword_t len, loff_t loff) {
    if(buf == NULL) {
        return 0;
    }

    return uart_rev_str(buf, len);
}

static word_t uart0_ioctl(uword_t cmd, void *arg) {
    struct uart_mode *p;
    struct uart_mode mode;

    if(cmd == UART_CMD_SETMODE) {
        if(arg == NULL) {
            return FALSE;
        }
        p = (struct uart_mode*)arg;
        mode.Baud = p->Baud;
        mode.DataLen = p->DataLen;
        mode.Parity = p->Parity;
        mode.StopLen = p->StopLen;
        init_uart_args(mode);
    } else if(cmd == UART_CMD_RESET)
    {
        bufClear(puart0->prevbuf);
    }
    return TRUE;
}

static void uart0_release(void) {
    semDel(0, puart0->psem);
    if(puart0->prevbuf != NULL) {
        bufDel(puart0->prevbuf);
    }
    if(puart0->psndbuf != NULL) {
        bufDel(puart0->psndbuf);
    }
    kfree(puart0);
    puart0 = NULL;
}

struct chdev_operations uart0_op = {
    uart0_open,
    uart0_close,
    NULL,
    uart0_write,
    uart0_read,
    uart0_ioctl,
    uart0_release,
};

void uart_register(void) {
    struct uart_mode mode;

    mode.DataLen = 8;
    mode.StopLen = 1;
    mode.Parity = UART_PARITY_NONE;
    mode.Baud = 115200;

    uart0_init(mode);
    chdevRegister(UART0_MID, UART0_SID, "uart0", &uart0_op);
}

